package com.jeeplus.modules.project.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.project.dao.ProjectMidteamExamDao;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectApply;
import com.jeeplus.modules.project.entity.ProjectMidteamExam;

/**
 * 中期检查Service
 * 
 * @author jacky
 * @version 2018-01-10
 */
@Service
@Transactional(readOnly = true)
public class ProjectMidteamExamService extends CrudService<ProjectMidteamExamDao, ProjectMidteamExam> {
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;

	public ProjectMidteamExam get(String id) {
		return super.get(id);
	}

	public List<ProjectMidteamExam> findList(ProjectMidteamExam projectMidteamExam) {
		return super.findList(projectMidteamExam);
	}

	public Page<ProjectMidteamExam> findPage(Page<ProjectMidteamExam> page, ProjectMidteamExam projectMidteamExam) {
		return super.findPage(page, projectMidteamExam);
	}

	@Transactional(readOnly = false)
	public void save(ProjectMidteamExam projectMidteamExam) {
		// 申请发起
		if (StringUtils.isBlank(projectMidteamExam.getId())) {
			projectMidteamExam.preInsert();
			dao.insert(projectMidteamExam);
			// 启动流程
			actTaskService.startProcess(ActUtils.PD_PROJECT_MIDTERM[0], ActUtils.PD_PROJECT_MIDTERM[1],
					projectMidteamExam.getId(), "【进度跟踪】"+projectMidteamExam.getBptInfo().getCourseName());
			//流程启动完毕 更新基本项目信息状态
		/*	try {
				//这里为什么要设置6呢， 先给注释了
				projectMidteamExam.getBptInfo().setStatus(6); //表示已经开始进行中期检查了
				baseProjectInfoService.updateStatus(projectMidteamExam.getBptInfo());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			
		}
		// 重新编辑申请
		else {
			projectMidteamExam.preUpdate();
			dao.update(projectMidteamExam);

			projectMidteamExam.getAct().setComment(("yes".equals(projectMidteamExam.getAct().getFlag()) ? "[重申] " : "[销毁] ")
					+ projectMidteamExam.getAct().getComment());
			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(projectMidteamExam.getAct().getFlag()) ? "1" : "0");
			actTaskService.complete(projectMidteamExam.getAct().getTaskId(), projectMidteamExam.getAct().getProcInsId(),
					projectMidteamExam.getAct().getComment(), projectMidteamExam.getBptInfo().getCourseName(), vars);
		}
	}

	@Transactional(readOnly = false)
	public void delete(ProjectMidteamExam projectMidteamExam) {
		super.delete(projectMidteamExam);
	}
	
	@Transactional(readOnly = false)
	public void approveSave(ProjectMidteamExam projectMidteamExam) {
		// 设置意见
		projectMidteamExam.getAct().setComment(("yes".equals(projectMidteamExam.getAct().getFlag())?"[同意] ":"[驳回] ")+projectMidteamExam.getAct().getComment());
		
		projectMidteamExam.preUpdate();
		
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = projectMidteamExam.getAct().getTaskDefKey();
		// 审核环节
		if ("dept-review".equals(taskDefKey)){
			projectMidteamExam.setHostDeptAdvice(projectMidteamExam.getAct().getComment());
			if("yes".equals(projectMidteamExam.getAct().getFlag())){
				projectMidteamExam.setStatus(1); //通过检查
			}else {
				projectMidteamExam.setStatus(2); //不通过检查
			}
		}
		/*else if ("dept-leader-review".equals(taskDefKey)){
			projectMidteamExam.setHostDeptLeaderAdvice(projectMidteamExam.getAct().getComment());
		}
		else if ("sci-research-magt-review".equals(taskDefKey)){
			projectMidteamExam.setCheckTeamAdvice(projectMidteamExam.getAct().getComment());
		}
		else if ("big-boss-review".equals(taskDefKey)){
			projectMidteamExam.setBigBossAdvice(projectMidteamExam.getAct().getComment());
			if("yes".equals(projectMidteamExam.getAct().getFlag())){
				projectMidteamExam.setStatus(1); //通过检查
			}
		}*/
		// 未知环节，直接返回
		else{
			return;
		}
		//更新数据
		dao.update(projectMidteamExam);
		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(projectMidteamExam.getAct().getFlag())? "1" : "0");
		actTaskService.complete(projectMidteamExam.getAct().getTaskId(), projectMidteamExam.getAct().getProcInsId(), projectMidteamExam.getAct().getComment(), vars);
	}

}